﻿// -----------------------------------------------------------------------
// <copyright file="TestAD.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace TestProject.FunWithAD
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.DirectoryServices.AccountManagement;
    using Microsoft.Interop.Security.AzRoles;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class TestAD
    {
        public static void Test()
        {
            PrincipalContext context = new PrincipalContext(ContextType.ApplicationDirectory, "sl20120120.tourisme.gouv.qc.ca", "DC=dev,DC=Tourisme,DC=gouv,DC=qc,DC=ca", ContextOptions.SimpleBind);
            Console.WriteLine("Log AdamUser : {0}", context.ValidateCredentials("AdamUser1", "Welcome1"));

            Console.WriteLine("Log AdamUser : {0}", context.ValidateCredentials("domirich", "B1yfbu11;"));
            var up = UserPrincipal.FindByIdentity(context, "AdamUser1");
            var p = UserProxyPrincipal.FindByIdentity(context, "domirich");
            Console.WriteLine(p.DistinguishedName);
            Console.WriteLine(p.Sid);

            var grp = GroupPrincipal.FindByIdentity(context, "GACC_Consultant_WEB");
            Console.WriteLine(grp.DistinguishedName);
            foreach (Principal pr in grp.Members)
            {
                Console.WriteLine(pr.DisplayName);
            }

            Console.WriteLine(" -------------- Groupe de p ----------------");
            Console.WriteLine(p.IsMemberOf(grp));
            foreach (Principal group in p.GetGroups())
            {
                Console.WriteLine("{0} Sid : {1}", group.Name, group.Sid);
            } 
            

            //PrincipalSearcher ps = new PrincipalSearcher(new UserPrincipal(context));
            //PrincipalSearchResult<Principal> result = ps.FindAll();
            //Console.WriteLine(" -------------- user ----------------");
            //Console.WriteLine(result.Count());
            //foreach (Principal r in result)
            //{
            //    Console.WriteLine(r.DistinguishedName);
            //}
            //Console.WriteLine(" -------------- userProxy ----------------");
            //ps.QueryFilter = new UserProxyPrincipal(context);
            //foreach (var r in ps.FindAll())
            //{
            //    Console.WriteLine(r.DistinguishedName);
            //}

            //Console.WriteLine(" -------------- AzMan ----------------");
            //AzAuthorizationStore AzStore = new AzAuthorizationStore();
            //AzStore.Initialize(0, "msldap://or1009-006.tourisme.gouv.qc.ca/CN=AzManADAMStore,DC=PreuveConcept,DC=local", null);
            //IAzApplication azApp = AzStore.OpenApplication("ApplicationTest");
            //IAzRole role = azApp.OpenRole("Utilisateurs de l'Application", null);
            ////role.AddMember(up.Sid.ToString(), null);
            ////role.DeleteMember(p.Sid.ToString(), null);
            //role.Submit(0, null);

            //IAzClientContext2 ctx = (IAzClientContext2)azApp.InitializeClientContextFromStringSid(p.Sid.ToString(), 0);
            //var scopes = new object[1];
            //scopes[0] = (object)"";
            //var operations = new object[1];
            //operations[0] = 1;
            //object[] resultObjects = (object[])ctx.AccessCheck("AccessCheck", scopes, operations, null, null, null, null, null);
            //foreach (int iRes in resultObjects)
            //{
            //    Console.WriteLine("*********************************");

            //    if (iRes == 0)
            //    {

            //        Console.Out.WriteLine("ACCESS GRANTED");

            //    }

            //    else
            //    {

            //        Console.Out.WriteLine("ACCESS DENIED");

            //    }

            //    Console.WriteLine("*********************************");
            //}
            //Console.WriteLine();
        }
    }
}
